from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework import permissions
from apps.HttpTestcase.models import Envs
from apps.HttpTestcase.serializers import EnvModeSerializer, ReadsSerializer
from rest_framework.decorators import action
from apps.HttpTestcase.filters.env import EnvsFilters
from utils.response import SuccessResponse
from utils.viewsets import ModelViewSet


class EnvsViewSet(ModelViewSet):
    perms_map = {'get': '*', 'post': 'envs_create',
                 'put': 'envs_update', 'delete': 'envs_delete'}
    queryset = Envs.objects.filter(is_delete=False)
    serializer_class = EnvModeSerializer
    # 指定过滤引擎
    # filter_fields = [DjangoFilterBackend]
    filter_fields = ['id', 'name']
    filter_class = EnvsFilters
    # 指定权限类
    # permission_classes = [permissions.IsAuthenticated]

    def perform_destroy(self, instance):
        instance.is_delete = True
        instance.save()  # 逻辑删除

    @action(methods=['get'], detail=False)
    def names(self, request, pk=None):
        queryset = self.get_queryset()
        serializer = self.get_serializer(instance=queryset, many=True)
        # ResponsePackaging(200, serializer.data.data, '查询成功！')
        return SuccessResponse(data={'data': serializer.data}, msg='查询成功！')
